import 'package:flutter/material.dart';
import 'package:flutter_learn/learn/getx/LearnController.dart';
import 'package:get/get.dart';

class NumberCounter extends StatefulWidget {
  const NumberCounter({Key? key}) : super(key: key);

  @override
  _NumberCounterState createState() => _NumberCounterState();
}

class _NumberCounterState extends State<NumberCounter> {
  ContentController controller = Get.put(ContentController());
  ContentController controller2 = Get.put(ContentController());

  @override
  void initState() {
    super.initState();
    print(controller == controller2);
    controller.addListener(() {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("点击了")));
    });
  }

  @override
  Widget build(BuildContext context) {
    print("rebuild");
    return Scaffold(
      appBar: AppBar(title: Text("GetXContainer")),
      ///所有的包含在一起
      body: GetBuilder<ContentController>(
        builder: (cxt) => Column(
          children: [
            Container(
              height: 100,
              alignment: Alignment.center,
              child: Text("${controller.number}"),
            ),
            Container(
              height: 100,
              alignment: Alignment.center,
              child: Text("${controller.name}"),
            ),
            TextButton(
              onPressed: () {
                controller.change(DateTime.now().toString());
              },
              child: Text("时间"),
            )
          ],
        ),
      ),
      floatingActionButton: ElevatedButton(
        child: Text("增加"),
        onPressed: () {
          controller.increase();
        },
      ),
    );
  }

  @override
  void dispose() {
    ///释放状态
    Get.delete<ContentController>();
    super.dispose();
  }
}
